package com.beiing.tianshuai.tianshuai.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.style.ReplacementSpan;
import android.util.DisplayMetrics;

import com.beiing.tianshuai.tianshuai.util.DisplayUtil;

/**
 * Created by LZQ on 2017/11/30.
 */

public class RadiusBackgroundSpan extends ReplacementSpan {

    private Context mContext;
    private int mSize;
    private int mColor;
    private int textColor;
    private int mRadius;

    public RadiusBackgroundSpan(Context mContext, int mColor, int textColor, int mRadius) {
        this.mContext = mContext;
        this.mColor = mColor;
        this.textColor = textColor;
        this.mRadius = mRadius;
        DisplayMetrics displayMetrics = new DisplayMetrics();
    }

    @Override
    public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
        mSize = (int) (paint.measureText(text, start, end) - 10);
        return mSize;
    }

    @Override
    public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
        int color = paint.getColor();
        paint.setColor(mColor);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        RectF oval = new RectF(x + mRadius -8, y + paint.ascent() + 6, x + mSize + 8, y + paint.descent() - 6);
        canvas.drawRoundRect(oval, mRadius, mRadius, paint);
        paint.setColor(textColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(DisplayUtil.sp2px(mContext, 11));
        paint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(text, start, end, x + mSize / 2 + mRadius / 2, y - 3, paint);
    }
}
